layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'layedit', 'laydate', 'upload'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        upload = layui.upload;
    $ = layui.jquery;

    //创建一个编辑器
    var editIndex = layedit.build('news_content');
    var addNewsArray = [], addNews;


    /**
     * 监听提交 添加数据
     */
    form.on('submit(add)', function (data) {

        if ($("#userIp").val() == '') {
            $.message({
                message: '用户IP不能为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }

        if ($("#userName").val() == '') {
            $.message({
                message: '用户名不能为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }

        if ($("#password").val() == '') {
            $.message({
                message: '用户密码不能为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }

        if ($("#email").val() == '') {
            $.message({
                message: '用户邮箱不能为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }

        if ($("#userAge").val() == '') {
            $.message({
                message: '用户年龄为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }

        if ($("#telephoneNumber").val() == '') {
            $.message({
                message: '用户电话为空',
                type: 'warning',
                showClose: true
            });
            return false;
        }


        console.log(data.field)
        layer.confirm("确认要添加吗?", {
            yes: function (index, layero) {
                $.ajax({
                    type: "POST",
                    url: "/users/save",
                    data: data.field,
                    datatype: "json",
                    success: function (data) {
                        console.log(data);

                        if (data.code == 101) {
                            $.message({
                                message: data.msg,
                                type: 'warning',
                                showClose: true
                            });
                            return false;
                        }

                        if (data.code == 100) {
                            $.message({
                                message: data.msg,
                                type: 'warning',
                                showClose: true
                            });
                            return false;
                        }

                        $.message({
                            message: data.msg,
                            type: 'success',
                            showClose: true
                        });

                        /* window.parent.location.reload();*/
                    },
                    error: function () {
                        $.message({
                            message: 'boom..',
                            type: 'error',
                            showClose: true
                        });
                    }
                });
                layer.close(index);
            }
        });

        return false;
    })

})
